package com.cloudfun.campusshare.configuration.config;

import com.cloudfun.campusshare.common.constant.RabbitConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Huqin on 2020/3/30
 */
@Configuration
public class RabbitConfig {

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, RabbitMsgCallback rabbitMsgCallback) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        rabbitTemplate.setConfirmCallback(rabbitMsgCallback);
        rabbitTemplate.setReturnCallback(rabbitMsgCallback);
        return rabbitTemplate;
    }

    // 设备状态
    @Bean
    public Queue deviceStatusMsgQueue() {
        return new Queue(RabbitConstants.Queue.DEVICE_STATUS);
    }

    @Bean
    FanoutExchange deviceStatusMsgExchange() {
        return new FanoutExchange(RabbitConstants.Exchange.DEVICE_STATUS);
    }

    @Bean
    Binding deviceStatusMsgBinding() {
        return BindingBuilder.bind(deviceStatusMsgQueue()).to(deviceStatusMsgExchange());
    }

    // 设备上下线
    @Bean
    public Queue deviceEventMsgQueue() {
        return new Queue(RabbitConstants.Queue.DEVICE_EVENT);
    }

    @Bean
    FanoutExchange deviceEventMsgExchange() {
        return new FanoutExchange(RabbitConstants.Exchange.DEVICE_EVENT);
    }

    @Bean
    Binding deviceEventMsgBinding() {
        return BindingBuilder.bind(deviceEventMsgQueue()).to(deviceEventMsgExchange());
    }

    // 设备告警
    @Bean
    public Queue deviceFaultMsgQueue() {
        return new Queue(RabbitConstants.Queue.DEVICE_FAULT);
    }

    @Bean
    FanoutExchange deviceFaultMsgExchange() {
        return new FanoutExchange(RabbitConstants.Exchange.DEVICE_FAULT);
    }

    @Bean
    Binding deviceFaultMsgBinding() {
        return BindingBuilder.bind(deviceFaultMsgQueue()).to(deviceFaultMsgExchange());
    }

    //设备信息
    @Bean
    public Queue deviceFirmwareMsgQueue() {
        return new Queue(RabbitConstants.Queue.DEVICE_FIRMWARE);
    }

    @Bean
    FanoutExchange deviceFirmwareMsgExchange() {
        return new FanoutExchange(RabbitConstants.Exchange.DEVICE_FIRMWARE);
    }

    @Bean
    Binding deviceFirmwareMsgBinding() {
        return BindingBuilder.bind(deviceFirmwareMsgQueue()).to(deviceFirmwareMsgExchange());
    }

    @Bean(name = "delayQueue")
    public Queue delayQueue(){
        return new Queue(RabbitConstants.Queue.DELAY_QUEUE_NAME);
    }

    @Bean
    public CustomExchange delayExchange(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(RabbitConstants.Exchange.DELAY_EXCHANGE_NAME,"x-delayed-message",true,false,args);
    }

    @Bean
    public Binding bindQueue2Exchange(Queue delayQueue,CustomExchange customExchange){
        return BindingBuilder.bind(delayQueue).to(customExchange).with(RabbitConstants.routingKey.DELAY_ROUTING_KEY).noargs();
    }
}
